import {createCanvas, circle, fill, stroke, line, vector2} from '/static/spring/board.js'

class Particle {
    constructor(x, y) {
        this.position = vector2(x, y)
        this.mass = 1
        this.velocity = vector2()
        this.accelelation = vector2()
        this.locked = false
        this.radius = 10
    }

    applyForce(force) {
        this.accelelation.add(force.clone().scale(1/this.mass))
    }

    update () {
        if (!this.locked) {
            this.velocity.add(this.accelelation)
            this.position.add(this.velocity)
            this.accelelation.mul(0)
            this.velocity.mul(0.99)
        }
    }

    show () {
        circle(this.position.x, this.position.y, this.radius)
        fill()
        stroke()
    }
}


class Spring {
    constructor(a, b, restLength = 100, k = 0.1) {
        this.a = a
        this.b = b
        this.restLength = restLength
        this.k = k
    }
    update() {
        let force = this.b.position.clone().sub(this.a.position)
        let x = force.length - this.restLength
        this.b.applyForce(force.normalize().mul(-this.k * x))
        this.b.update()
        this.a.applyForce(force.mul(-1))
        this.a.update()
    }
    show() {
        // this.a.show()
        // this.b.show()
        line(this.a.position.x, this.a.position.y, this.b.position.x, this.b.position.y, 4)
        stroke()
    }
}

class Mouse {
    constructor(domElement) {
        if (domElement === undefined) console.warn('PickContrls: The parameter "domElement" is now mandatory.');
        if (domElement === document) console.error('PickContrls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.');

        this.domElement = domElement;
        this.position = vector2()
        this.isPress = false
        this._pixelRatio = 1

        this.setPixelRatio = function (pixelRatio) {
            this._pixelRatio = pixelRatio
        }

        const scope = this
        scope.domElement.addEventListener('pointerdown', onPointerDown);
        function onPointerDown(event) {
            if (scope.enabled === false) return;
            // if (Object.keys(pointers).length === 0) {
            // }

            scope.domElement.setPointerCapture(event.pointerId);
            scope.domElement.addEventListener('pointermove', onPointerMove);
            scope.domElement.addEventListener('pointerup', onPointerUp);

            scope.isPress = true
            scope.position.x = event.offsetX * scope._pixelRatio
            scope.position.y = event.offsetY * scope._pixelRatio
        }

        function onPointerMove(event) {
            if (scope.enabled === false) return;
            scope.position.x = event.offsetX * scope._pixelRatio
            scope.position.y = event.offsetY * scope._pixelRatio
        }


        function onPointerUp(event) {
            if (scope.enabled === false) return;
            scope.isPress = false

            // if (Object.keys(pointers).length === 0) {
            // }
            scope.domElement.releasePointerCapture(event.pointerId);
            scope.domElement.removeEventListener('pointermove', onPointerMove);
            scope.domElement.removeEventListener('pointerup', onPointerUp);

        }

    }
}

export {Particle, Spring, Mouse}